package cn.virens.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.session.MapSessionRepository;
import org.springframework.session.config.annotation.web.http.EnableSpringHttpSession;

import net.sf.ehcache.CacheManager;

/**
 * 配置Spring使用Ecache作为缓存
 * 
 * @作者 :loioi
 * @创建时间 :2017年7月17日
 */
@Configuration
@EnableSpringHttpSession
public class SpringCacheEhcacheConfig {
	/**
	 * Spring Session
	 * 
	 * @return
	 */
	@Bean
	public MapSessionRepository sessionRepository() {
		return new MapSessionRepository();
	}

	/**
	 * 缓存配置
	 * 
	 * @return
	 */
	@Bean("cacheManagerFactoryBean")
	public EhCacheManagerFactoryBean cacheManagerFactoryBean() {
		EhCacheManagerFactoryBean factoryBean = new EhCacheManagerFactoryBean();
		factoryBean.setConfigLocation(new ClassPathResource("spring_cache.xml"));
		factoryBean.setShared(true);

		return factoryBean;
	}

	/**
	 * 缓存管理器
	 * 
	 * @return
	 */
	@Bean("springCacheManager")
	public EhCacheCacheManager ehCacheCacheManager(@Qualifier("cacheManagerFactoryBean") CacheManager cacheManager) {
		return new EhCacheCacheManager(cacheManager);
	}

}
